//
//  VisitorLoginView.swift
//  Weibo
//
//  Created by apple on 16/1/10.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
//导入SnapKit命名空间
import SnapKit
@objc protocol visitorViewDelegate : NSObjectProtocol{

  func willLogin()
 optional  func willregister()
    
}
class VisitorLoginView: UIView {
    
    
    weak var visitordelegate : visitorViewDelegate?
    func viewforsee(imageName: String?,tipText: String){
        
        if let img = imageName {
            largeHouse.image = UIImage(named:img)
            tipLabel.text = tipText
            circleIcon.hidden = true
        }else{
        
        
        }
     }
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置UI
        setupUI()
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
    }

    //默认实现报错
    //当程序员通过 sb / xib 加载该视图 程序就会崩溃
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private func setupUI() {
        //添加子视图
        
        addSubview(circleIcon)
        addSubview(backView)

        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //设置自动布局 苹果手码布局 通过 VFL NSLayoutConstraint
        /*
        1.item: 表示要设置约束的对象
        2.attribute: 需要添加约束的属性
        3.relatedBy: 参照方法 一般使用 精准参照 .Equal\
        4.toItem: 约束被参照的对象
        attribute:被参照约束的属性
        
        
        view1.attr1 = view2.attr2 * multiplier + constant
        */
        //将约束对象添加到 父视图上
        //注意一点 自动布局能够生效 需要屏蔽frame布局方式
//        circleIcon.translatesAutoresizingMaskIntoConstraints = false
//        self.addConstraint(NSLayoutConstraint(item: circleIcon, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        /*
        添加约束 推荐 使用链式约束
        */
        //snp_makeConstraints 表示添加一条约束
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            //垂直居中 添加偏移量
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            //宽度约束
            make.width.equalTo(225)
            
        }
        backView.snp_makeConstraints { (make) -> Void in
            //            make.top.equalTo(self.snp_top)
            //            make.left.equalTo(self.snp_left)
            //            make.right.equalTo(self.snp_right)
            //和Masory 类似 支持链式设置约束的方式
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(registerBtn.snp_bottom)
        }
        
        
        //登陆按钮
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        

        
    }
    
    //懒加载获取所有的子视图
    //圈圈
    lazy var circleIcon: UIImageView = {
        
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
          let basic = CABasicAnimation(keyPath: "transform.rotation")
            basic.toValue = M_PI * 2
        basic.repeatCount = MAXFLOAT
        basic.duration = 10
        basic.removedOnCompletion = false
      img.layer.addAnimation(basic, forKey: nil)
          return img
        
        }()
    //大房子
    lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //提示文案
    lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.font = UIFont.systemFontOfSize(16)
        l.text = "登录后,别人评论你的微薄别人评论你的微薄别人评论你的微"
        //设置行数
        l.numberOfLines = 0
        l.textColor = UIColor.darkGrayColor()
        l.textAlignment = .Center
        //设置大小
        l.sizeToFit()
        return l
    }()
    @objc private func login(){
        visitordelegate?.willLogin()
    }
    @objc private func register(){
       visitordelegate?.willregister?()
    
    }
    //登录按钮
    lazy var loginBtn: UIButton = {
        let btn = UIButton()
        //设置按钮的文字
        btn.setTitle("登录", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    //注册按钮
    lazy var registerBtn: UIButton = {
        let btn = UIButton()
        //设置按钮的文字
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        let image = UIImage(named: "common_button_white_disable")!
        let w = Int(image.size.width * 0.5)
        let h = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
          btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()

}
